{extend name="normal_base" /}
{block name="navigation"}{/block}
{block name="body"}
<div class="x-body">
    <form class="layui-form" method="POST" action="{:url('edit')}" id="from_id">
        <!--username-->
        <div class="layui-form-item">
            <label for="username" class="layui-form-label">
                <span class="x-red">*</span>登录名
            </label>
            <div class="layui-input-inline">
                <input type="text" id="username" name="username" required="" lay-verify="required"
                       autocomplete="off" class="layui-input" value="{$user_info.username}">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>将会成为人员唯一的登入名
            </div>
        </div>

        <!--email-->
        <div class="layui-form-item">
            <label for="L_email" class="layui-form-label">
                邮箱
            </label>
            <div class="layui-input-inline">
                <input type="text" id="L_email" name="email" lay-verify="m_email"
                       autocomplete="off" class="layui-input" value="{$user_info.email}">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <!--<span class="x-red">*</span>-->
            </div>
        </div>

        <!--角色-->
        <div class="layui-form-item">
            <label class="layui-form-label"><span class="x-red">*</span>角色</label>
            <div class="layui-input-block">
                {foreach name="auth_group_list" item="vo"}
                <input type="checkbox" name="group_id[]" lay-skin="primary" title="{$vo.title}" value="{$vo.id}" {if condition="in_array($vo.id,$group_list)==true"}checked="checked"{/if} {if condition="$cannot_edit == true"}disabled{/if} class='group_id'] />
                {/foreach}
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <input type="hidden" name="id" value="{$user_info.id}">
                <button  class="layui-btn" lay-filter="edit" lay-submit="">
                    修改
                </button>
            </div>
        </div>
    </form>
</div>

{/block}

{block name="script"}
<script>
    layui.use(['form','layer'], function(){
        $ = layui.jquery;
        var form = layui.form
                ,layer = layui.layer;

        //自定义验证规则
        form.verify({
            nikename: function(value){
                if(value.length < 5){
                    return '昵称至少得5个字符啊';
                }
            }
            ,pass: [/(.+){6,12}$/, '密码必须6到12位']
            ,repass: function(value){
                if($('#L_pass').val()!=$('#L_repass').val()){
                    return '两次密码不一致';
                }
            }
            ,m_email:function (value) {
                if (value != "") {
                    //验证是否是邮箱
                    var emReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
                    if(!emReg.test(value)) {
                        return "请填写正确的邮箱地址!";
                    }
                }
            }
        });

        //监听提交
        form.on('submit(edit)', function(data){
            $("#from_id").ajaxSubmit({
                beforeSubmit:function () {
                    //表单提交前
                    if ($("input:checkbox[class='group_id']:checked").length == 0) {
                        //选中的角色数量为0
                        layer.msg('请为人员指定角色！',{icon:5,time:2000});
                        return false;
                    }
                },
                success:function (data) {
                    //console.log(JSON.stringify(data));
                    if (data.code == 1) {
                        layer.msg(data.msg,{icon:5,time:1500});
                    } else {
                        layer.alert("修改成功", {icon: 6},function () {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);
                            //刷新父层页面
                            parent.location.replace(parent.location.href);
                        });
                    }
                }
            });
            return false;
        });
    });
</script>
{/block}